﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace OgilvyOne.UKNow.BLL
{
    public class GetDocByURL
    {
        private HttpWebRequest hwrequst;
        private HttpWebResponse hwrespose;
        private Uri url;
        private string erroMsg = null;

        public GetDocByURL(string Url)
        {
            try
            {
                url = new Uri(Url);
                hwrequst = (HttpWebRequest)HttpWebRequest.Create(url);
                hwrequst.Method = "GET";
                //hwrequst.AllowAutoRedirect = false;
                hwrespose = (HttpWebResponse)hwrequst.GetResponse();
            }
            catch (Exception ex)
            {
                erroMsg = ex.Message;
            }
        }

        public string GetDoc()
        {
            if (erroMsg == null)
            {
                var respContent = string.Empty;
                var respStream = hwrespose.GetResponseStream();
                using (StreamReader reader = new StreamReader(respStream, Encoding.UTF8))
                {
                    respContent = reader.ReadToEnd();
                }
                hwrespose.Close();

                return respContent;
            }
            else
            {
                return erroMsg;
            }
        }
    }
}
